<?php

	/**
	 * @see Bike_Js_Ext_panel_Panel
	 */
	require_once 'Bike/Js/Ext/panel/Panel.php';


	class Bike_Js_Ext_form_Panel extends Bike_Js_Ext_panel_Panel
	{
		protected $_part = 'Panel';

		protected function initDefaultSettings()
		{
			parent::initDefaultSettings();

			$js = $this->js();
			$this->setSettings(array(
				'autoScroll' => true,
				'layout' => 'anchor',
				'defaults' => (object) array(
					'anchor' => '100%',
				),
				'activeItem' => 0,
				'buttonAlign' => 'center',
				'bodyPadding' => 5,
				'border' => 0,
				'listeners' => array(
	    		    'beforeaction' => $this->getJsListenersBeforeaction(),
	    			'actionfailed' => $this->getJsListenersActionfailed(),
					'actioncomplete' => $this->getJsListenersActioncomplete(),
	    		),
			));
		}

		/**
		 * Get JS listeners beforeaction
		 *
		 * @return Bike_Js_Interface_Js object
		 */
		protected function getJsListenersBeforeaction()
		{
			if(!$this->getParamCache(__METHOD__))
			{
				$js = $this->js();
				$this->setParamCache(__METHOD__,
					$js->function($js->form, $js->action)->_body(array(
	    		    	$js->form->loadMask->_val($js->Ext->LoadMask($js->form->owner)),
	    		    	$js->form->loadMask->setZIndex($js->form->loadMask->zIndexManager->getNextZSeed()),
    		    		$js->form->getFields()->each($js->function($js->item)->_body(array(
	    		    		$js->if($js->item->fireEvent('beforeaction', $js->item, $js->action)->_identity(false))->_body(array(
	    		    			$js->form->loadMask->hide(),
	    		    			$js->return(false),
	    		    		)),
	    		    	))),
	    		    	($js->isDebug() ? $js->alert($js->form->url) : ''),
	    		    ))
				);
			}
			return $this->getParamCache(__METHOD__);
		}

		/**
		 * Get JS listeners actionfailed
		 *
		 * @return Bike_Js_Interface_Js object
		 */
		protected function getJsListenersActionfailed()
		{
			if(!$this->getParamCache(__METHOD__))
			{
				$js = $this->js();
				$this->setParamCache(__METHOD__,
					$js->function($js->form, $js->action, $js->options)->_body(array(
    					($js->isDebug() ? $js->alert('actionfailed') : ''),
    					$js->form->loadMask->hide(),
    				))
				);
			}
			return $this->getParamCache(__METHOD__);
		}

		/**
		 * Get JS listeners actioncomplete
		 *
		 * @return Bike_Js_Interface_Js object
		 */
		protected function getJsListenersActioncomplete()
		{
			if(!$this->getParamCache(__METHOD__))
			{
				$js = $this->js();
				$this->setParamCache(__METHOD__,
					$js->function($js->form, $js->action, $js->options)->_body(array(
							($js->isDebug() ? $js->alert('actioncomplete') : ''),
					))
				);
			}
			return $this->getParamCache(__METHOD__);
		}
	}
